summaryrefslogtreecommitdiffstats
path: root/src/core/hle/kernel/k_scoped_lock.h
blob: 629a7d20dd670ccae1c21276e9dc5d6adc3f64c6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <concepts>
#include <memory>
#include <type_traits>

namespace Kernel {

template <typename T>
concept KLockable = !
std::is_reference_v<T>&& requires(T& t) {
                             { t.Lock() } -> std::same_as<void>;
                             { t.Unlock() } -> std::same_as<void>;
                         };

template <typename T>
    requires KLockable<T>
class KScopedLock {
public:
    explicit KScopedLock(T* l) : m_lock(*l) {}
    explicit KScopedLock(T& l) : m_lock(l) {
        m_lock.Lock();
    }

    ~KScopedLock() {
        m_lock.Unlock();
    }

    KScopedLock(const KScopedLock&) = delete;
    KScopedLock& operator=(const KScopedLock&) = delete;

    KScopedLock(KScopedLock&&) = delete;
    KScopedLock& operator=(KScopedLock&&) = delete;

private:
    T& m_lock;
};

} // namespace Kernel